/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    tim.c
  * @brief   This file provides code for the configuration
  *          of the TIM instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "tim.h"

/* USER CODE BEGIN 0 */
uint32_t tim_1s = 0;
uint32_t tim_500ms = 0;
uint32_t tim_200ms = 0;
uint32_t tim_100ms = 0;
uint32_t tim_50ms = 0;
uint32_t tim_10ms = 0;

uint8_t TIM_1S_FLAG = 0;
uint8_t TIM_500MS_FLAG = 0;
uint8_t TIM_200MS_FLAG = 0;
uint8_t TIM_100MS_FLAG = 0;
uint8_t TIM_50MS_FLAG = 0;
uint8_t TIM_10MS_FLAG = 0;
uint8_t TIM_1MS_FLAG = 0;
/* USER CODE END 0 */

TIM_HandleTypeDef htim6;

/* TIM6 init function */
void MX_TIM6_Init(void)
{

  /* USER CODE BEGIN TIM6_Init 0 */

  /* USER CODE END TIM6_Init 0 */

  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM6_Init 1 */

  /* USER CODE END TIM6_Init 1 */
  htim6.Instance = TIM6;
  htim6.Init.Prescaler = 72;
  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim6.Init.Period = 1000;
  htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM6_Init 2 */
  HAL_TIM_Base_Start_IT(&htim6);
  /* USER CODE END TIM6_Init 2 */

}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{

  if(tim_baseHandle->Instance==TIM6)
  {
  /* USER CODE BEGIN TIM6_MspInit 0 */

  /* USER CODE END TIM6_MspInit 0 */
    /* TIM6 clock enable */
    __HAL_RCC_TIM6_CLK_ENABLE();

    /* TIM6 interrupt Init */
    HAL_NVIC_SetPriority(TIM6_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(TIM6_IRQn);
  /* USER CODE BEGIN TIM6_MspInit 1 */

  /* USER CODE END TIM6_MspInit 1 */
  }
}

void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{

  if(tim_baseHandle->Instance==TIM6)
  {
  /* USER CODE BEGIN TIM6_MspDeInit 0 */

  /* USER CODE END TIM6_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_TIM6_CLK_DISABLE();

    /* TIM6 interrupt Deinit */
    HAL_NVIC_DisableIRQ(TIM6_IRQn);
  /* USER CODE BEGIN TIM6_MspDeInit 1 */

  /* USER CODE END TIM6_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance==TIM6)
	{
		// 1MS计时 进来标志位就为1
		if (TIM_1MS_FLAG == 0)
		{
			TIM_1MS_FLAG = 1;
		}

		// 10MS计时
		if(tim_10ms<10 && TIM_10MS_FLAG == 0) // 计时小于1000并且flag为0时 自加
		{
			tim_10ms++;
		}
		else // 当tim大于等于10 或者flag = 1 时计时为0
		{
			tim_10ms = 0;
			TIM_10MS_FLAG = 1;
		}

		// 50MS计时
		if(tim_50ms<10 && TIM_50MS_FLAG == 0) // 计时小于1000并且flag为0时 自加
		{
			tim_50ms++;
		}
		else // 当tim大于等于50 或者flag = 1 时计时为0
		{
			tim_50ms = 0;
			TIM_50MS_FLAG = 1;
		}

		// 100MS计时
		if(tim_100ms<10 && TIM_100MS_FLAG == 0) // 计时小于1000并且flag为0时 自加
		{
			tim_100ms++;
		}
		else // 当tim大于等于100 或者flag = 1 时计时为0
		{
			tim_100ms = 0;
			TIM_100MS_FLAG = 1;
		}

		// 200MS计时
		if(tim_200ms<10 && TIM_200MS_FLAG == 0) // 计时小于1000并且flag为0时 自加
		{
			tim_200ms++;
		}
		else // 当tim大于等于200 或者flag = 1 时计时为0
		{
			tim_200ms = 0;
			TIM_200MS_FLAG = 1;
		}

		// 500MS计时
		if(tim_500ms<10 && TIM_500MS_FLAG == 0) // 计时小于1000并且flag为0时 自加
		{
			tim_500ms++;
		}
		else // 当tim大于等于500 或者flag = 1 时计时为0
		{
			tim_500ms = 0;
			TIM_500MS_FLAG = 1;
		}

		// 1S计时
		if(tim_1s<1000 && TIM_1S_FLAG == 0) // 计时小于1000并且flag为0时 自加
		{
			tim_1s++;
		}
		else // 当tim大于等于1000 或者flag = 1 时计时为0
		{
			tim_1s = 0;
			TIM_1S_FLAG = 1;
		}

	}
}
/* USER CODE END 1 */
